<template>
  <div class="login_pages">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      class="login-ruleForm"
    >
      <h3>叩丁狼电商管理系统</h3>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="ruleForm.username"
          autocomplete="off"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item prop="pwd">
        <el-input
          type="password"
          v-model="ruleForm.pwd"
          autocomplete="off"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loginFn">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { LoginApi, GetinfoApi } from "../../request/api";
import { ADDUSERINFO } from "../../store/mutations-type";
export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" },
        ],
        pwd: [{ required: true, message: "密码不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    loginFn() {
      this.$refs.ruleForm
        .validate()
        .then((vali) => {
          // 校验成功
          console.log(vali);
          LoginApi({
            password: this.ruleForm.pwd,
            username: this.ruleForm.username,
          }).then((res) => {
            console.log(res);
            if (res.code === 200) {
              localStorage.setItem(
                "token",
                res.data.tokenHead + res.data.token
              );
              // 用户信息获取
              GetinfoApi().then((res) => {
                if (res.code === 200) {
                  this.$router.push("/homepage");
                  this.$store.commit(ADDUSERINFO, res.data);

                }
              });
            }
          });
        })
        .catch((err) => {
          // 校验失败
          console.log(err);
        });
    },
  },
};
</script>
 
<style lang = "less" scoped>
.login_pages {
  height: 100%;
  background-color: #2d3a4b;

  .login-ruleForm {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    h3 {
      padding: 15px 0;
      color: #fff;
    }
    .el-form-item {
      width: 200px;
      height: 30px;
    }
    .el-button {
      width: 200px;
    }
  }
}
</style>